package com.ruoyi.pcwl.utils;


import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 *
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算相差天数
     */
    public static int differentDaysByMillisecond(Date date1, Date date2)
    {
        return Math.abs((int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24)));
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor)
    {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor)
    {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }


    static String format = "yyyy-MM-dd HH:mm:ss";

    //取当前时间字符串
    public static String getDateStr(Date date) {
        format= "yyyyMMdd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }


    //取当前时间字符串
    public static String getNowTimeStr(Date date) {
        format= "yyyyMMdd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    //取当前时间字符串
    public static String getNowTimeStr() {
        format= "yyyyMMdd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }


    /*
     * 根据日期获取前一月的年份
     */
    public static Integer getLastMonthYear(Date date) {

        Calendar c = Calendar.getInstance();

        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return c.get(Calendar.YEAR);
    }

    /*
     * 根据日期获取前一月的日期
     */
    public static Date getLastMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);

        return calendar.getTime();
    }

    /*
     * 根据日期获取前一天的日期
     */
    public static Date getTomorrow(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);

        return calendar.getTime();
    }

    /*
     * 根据日期获取前一天的日期
     */
    public static Date getYesterday(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return calendar.getTime();
    }


    /*
     * 根据日期获取前一天的日期
     */
    public static Date getLastDate(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return calendar.getTime();
    }



    /*
     * 输入日期字符串比如2021-10，返回指定月份前一个月最后一天的Date
     */
    public static Integer getMaxDateMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

        return calendar.get(Calendar.DAY_OF_MONTH);
    }


    /*
     * 输入日期字符串比如2021-10，返回指定月份前一个月最后一天的Date
     */
    public static Date getMaxDateMonth(String month) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date nowDate = simpleDateFormat.parse(month);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowDate);
            calendar.add(Calendar.MONTH, -1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            return calendar.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 输入日期字符串，返回指定当月后一个月第一天的Date
     */
    public static Date getMinDateMonth(String month) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            Date nowDate = simpleDateFormat.parse(month);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(nowDate);
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            return calendar.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    //    把时间戳转换成"yyyy-MM-dd HH:mm:ss"这种格式的字符串
    public static String formatString(Long time) {
        String dateTime = new SimpleDateFormat(format).format(new Date(time));
        return dateTime;
    }

    public static String formatString(Date time) {
        String dateTime = new SimpleDateFormat(format).format(time);
        return dateTime;
    }

    public static String formatDate2String(Date time) {
        String dateTime = new SimpleDateFormat(YYYY_MM_DD).format(time);
        return dateTime;
    }

    public static String formatDateTime2String(Date time) {
        String dateTime = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(time);
        return dateTime;
    }

    //把时间戳转换成指定格式的字符串
    public static String formatString(Long time,String format) {
        String dateTime = new SimpleDateFormat(format).format(new Date(time));
        return dateTime;
    }

    //把"yyyy-MM-dd HH:mm:ss"这种格式的字符串转换成时间
    public static Date getDateTime(String datetime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    /**
     * 把指定格式的字符串转换成时间
     * @param datetime
     * @param format
     * @return
     */
    public static Date getDateTime(String datetime,String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(datetime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    /**
     * 相差天数（隔24小时为相差1天，否则为0天）
     * @param fDate
     * @param oDate
     * @return d
     */
    public static int getIntervalDays(Date fDate, Date oDate) {
        if (null == fDate || null == oDate) {
            return -1;
        }
        long intervalMilli = oDate.getTime() - fDate.getTime();
        return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }

    /**
     * 相差小时数
     * @param fDate
     * @param oDate
     * @return d
     */
    public static int getIntervalHours(Date fDate, Date oDate) {
        if (null == fDate || null == oDate) {
            return -1;
        }
        long intervalMilli = oDate.getTime() - fDate.getTime();
        return (int) (intervalMilli / (60 * 60 * 1000));
    }

    /**
     * 获取今日开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setDayRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        //设置秒为0
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        condition.put("startTime", calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取昨天开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setYesterdayRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        condition.put("startTime", calendar.getTime());
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        condition.put("stopTime", calendar.getTime());


    }

    /**
     * 获得最近三天的开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setThreeRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime", calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, -72);
        condition.put("startTime", calendar.getTime());
    }

    /**
     * 获得近一周的开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setDaySevenRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime", calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, -168);
        condition.put("startTime", calendar.getTime());
    }

    /**
     * 获得近一月的开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setDayTRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 24);
        condition.put("stopTime", calendar.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, -720);
        condition.put("startTime", calendar.getTime());
    }

    /**
     * 获取本周开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setWeekRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());

    }

    /**
     * 获取上周开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setLastWeekRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        condition.put("startTime", calendar.getTime());
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取本月开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setMonthRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        // calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取上月开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setLastMonthRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.DAY_OF_WEEK, 7);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //calendar.set(Calendar.HOUR_OF_DAY, 24);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取本季度开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setQuarterRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.MONTH, 3);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取上季度开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setLastQuarterRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                calendar.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                calendar.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                calendar.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                calendar.set(Calendar.MONTH, 9);
            calendar.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.add(Calendar.MONTH, -3);
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.MONTH, 3);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取近半年开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setHalfYearRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH, -6);
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取上半年开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setUpHalfYearRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取下半年开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setNextHalfYearRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.MONTH, 6);
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.MONTH, 6);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }


    /**
     * 获取本年开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setYearRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * 获取上一年开始时间和结束时间
     *
     * @param condition
     * @return
     */
    public static void setLastYearRange(Map<Object, Object> condition) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.YEAR, -1);
        condition.put("startTime", calendar.getTime());
        calendar.add(Calendar.YEAR, 1);
        calendar.set(Calendar.SECOND, -1);
        condition.put("stopTime", calendar.getTime());
    }

    /**
     * （"startTime" -> "Fri Jun 08 00:00:00 CST 2018"）String的Date转为String类型的date
     */
    public static String strToformatDate(String date) {
        String str = "";
        Date date1 = new Date();
        try {
            DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            date1 = df.parse(date);
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            str = df.format(date1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }


    public static Date timeToTalculate(Date date,String years,int amount){
        Calendar c = Calendar.getInstance();
        //设置时间
        c.setTime(date);
        if(years.equals("day")){
            //时间加一天
            c.add(Calendar.DAY_OF_MONTH, amount);
            //get获取时间
            return c.getTime();
        }else if(years.equals("year")){
            c.add(Calendar.YEAR, amount);
            return c.getTime();
        }
        return null;
    }


    public void test4(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        /*
         * 获取15天后是多少号?
         *
         * add(int field,int n)
         * 为当前calendar指定时间分量上累加给定值
         * 若传入的是负数，则是累减
         */
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 15);  //给当前时间加15天
        //System.out.println(calendar.getTime());
        System.out.println(sdf.format(calendar.getTime()));


        /*
         * 三个月零十天后的那周的星期三是几号?
         */
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 3); //给当前时间加3个月
        cal.add(Calendar.DAY_OF_YEAR, 10);  //给当前时间加10天
        //得出三个月十天后的日期，然后将日期设为当周星期三
        cal.set(Calendar.DAY_OF_WEEK, 4); //1是周天，所以4是周三
        //System.out.println(cal.getTime());
        System.out.println(sdf.format(cal.getTime()));


        /*
         * 获取某个时间分量所允许的最大值
         * 获取今年最大天数
         */
        Calendar cal1 = Calendar.getInstance();
        int days = cal1.getActualMaximum(Calendar.DAY_OF_YEAR);
        System.out.println("今年最大天数为："+days+"天");

        /*
         * 一周的第一天是星期日
         * 获取当前是星期几
         */
        Calendar cal2 = Calendar.getInstance();
        int w = cal2.get(Calendar.DAY_OF_WEEK);
        System.out.println("今天是星期："+(w==1?7:w-1));
    }
}
